package com.hongsuan.pplive.mobileAPI.sys;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cnjson.common.config.Global;
import com.cnjson.common.utils.CacheUtils;
import com.cnjson.common.utils.DesUtils;
import com.cnjson.common.utils.MD5Utils;
import com.cnjson.common.web.BaseController;
import com.hongsuan.pplive.mobileAPI.utils.JsonUtils;
import com.hongsuan.pplive.mobileAPI.vo.AppSdkInfo;
import com.hongsuan.pplive.mobileAPI.vo.CheckStatus;
import com.hongsuan.pplive.mobileAPI.vo.ReturnObject;
import com.hongsuan.pplive.modules.sys.entity.SysAppSetting;
import com.hongsuan.pplive.modules.sys.service.SysAppSettingService;

@Controller
@RequestMapping("/v2/sys")
public class SysConfigAPIController extends BaseController {

	private static final String TOKEN_KEY = Global.getConfig("token");// .getTokenKey();
	private static final String sdkAppId = Global.getConfig("appId");// Global.getAppId();
	// private static final String expireTime = Global.getSigExpireTime();
	private static final String accountType = Global.getConfig("accountType");// .getAccountType();
	@Autowired
	private SysAppSettingService appSettingService;

	/**
	 * App注册验证
	 * 
	 * @param version
	 *            版本
	 * @param type
	 *            类型 Android或iOS
	 * @param key
	 *            通讯验证码串
	 * @return 返回JSON串 <code> 
	  	{
	"code" : 0,
	"message" : "",
	"version": 0.2
	"force": 0
	}</code>
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/appRegister")
	public @ResponseBody String appRegister(@RequestParam("version") String version, @RequestParam("type") String type,
			@RequestParam("key") String key) {
		CheckStatus status = new CheckStatus();
		status.setCode(1);
		status.setForce(0);
		status.setVersion(version);
		status.setMessage(Global.FAILED);

		String keyString = type + version + TOKEN_KEY;
		String password;
		try {
			password = MD5Utils.getMD5Sum(keyString);
			String result = DesUtils.decrypt(key, password);
			if (result.equalsIgnoreCase(type + "-" + version)) {
				String token = DesUtils.encrypt(TOKEN_KEY + sdkAppId, TOKEN_KEY);
				Integer ytype = 1;
				if (type.equalsIgnoreCase("android")) {
					ytype = 1;
				} else if (type.equalsIgnoreCase("ios")) {
					ytype = 2;
				} else {
					ytype = 0;
				}
				SysAppSetting app = appSettingService.getLastOne(ytype);
				if (app != null) {
					String localVersion = app.getVersion().toLowerCase();
					if (localVersion.startsWith("v")) {
						localVersion = localVersion.substring(1);
					}
					if (!localVersion.equalsIgnoreCase(version)) {
						status.setForce(1);
						status.setVersion(localVersion);
					}
					status.setDownloadUrl(app.getDownloadUrl());
				}
				status.setCode(0);
				status.setMessage(Global.SUCCESS);
				status.setAppId(sdkAppId);
				status.setToken(token);
			} else {
				status.setMessage(Global.FAILED);
			}
		} catch (Exception e) {
			status.setMessage(Global.FAILED);
			status.setCode(2);
			logger.error(e.getMessage());
		}
		return JsonUtils.toJson(status);
	}

	@RequestMapping(method = RequestMethod.GET, value = "/getAppConfig")
	public @ResponseBody String getAppConfig() {
		ReturnObject ret = new ReturnObject();
		try {
			AppSdkInfo info = (AppSdkInfo) CacheUtils.get("sys_config", sdkAppId);
			if (info == null) {
				info = new AppSdkInfo();
				info.setAccountType(accountType);
				info.setAppName("MDBOBO");
				info.setAppId(sdkAppId);
				CacheUtils.put("sys_config", sdkAppId, info);
			}
			ret.setCode(0);
			ret.setMessage(Global.SUCCESS);
			ret.setObject(info);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return ret.toString();

	}
}
